package com.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.UserDao;
import com.database.DBAccess;
import com.mysql.jdbc.PreparedStatement;
import com.vo.User;

public class UserDaoImpl implements UserDao {

	@Override
	public User findUser(User user) {
		User userinfo = new User();
		DBAccess db = new DBAccess();
		db.createConn();
		String sql = "select * from user where username='" + user.getUsername() + "' and password='"
				+ user.getPassword() + "' and type='" + user.getType() + "'";
		db.query(sql);
		ResultSet rs = db.getRs();
		try {
			if (rs.next()) {
				userinfo.setId(rs.getInt(1));
				userinfo.setUsername(rs.getString(2));
				userinfo.setPassword(rs.getString(3));
				userinfo.setQuestion(rs.getString(4));
				userinfo.setAnswer(rs.getString(5));
				userinfo.setType(rs.getString(6));
			} else {
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeRs();
		db.closeStm();
		db.closeConn();
		return userinfo;
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		DBAccess db = new DBAccess();
		List<User> usersList = new ArrayList<>();
		db.createConn();
		String sql = "select * from user";
		db.query(sql);
		ResultSet rs = db.getRs();
		try {
			while(rs.next()) {
				User userinfo = new User();
				userinfo.setId(rs.getInt(1));
				userinfo.setUsername(rs.getString(2));
				userinfo.setPassword(rs.getString(3));
				userinfo.setQuestion(rs.getString(4));
				userinfo.setAnswer(rs.getString(5));
				userinfo.setType(rs.getString(6));
				usersList.add(userinfo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeRs();
	        db.closeStm();
	        db.closeConn();
	    }
		return usersList;
	}

	@Override
	public List<User> findUserByName(String username) {
		// TODO Auto-generated method stub
		DBAccess db = new DBAccess();
		List<User> usersList = new ArrayList<>();
		db.createConn();
		String sql = "select * from user where username like '%" +username +"%'";
		db.query(sql);
		ResultSet rs = db.getRs();
		try {
			while(rs.next()) {
				User userinfo = new User();
				userinfo.setId(rs.getInt(1));
				userinfo.setUsername(rs.getString(2));
				userinfo.setPassword(rs.getString(3));
				userinfo.setQuestion(rs.getString(4));
				userinfo.setAnswer(rs.getString(5));
				userinfo.setType(rs.getString(6));
				usersList.add(userinfo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeRs();
	        db.closeStm();
	        db.closeConn();
	    }
		return usersList;
	}

	@Override
	public User findUserById(int id) {
		// TODO Auto-generated method stub
		DBAccess db = new DBAccess();
		User userinfo = new User();
		
		db.createConn();
		String sql = "select * from user where id='" + id + "'";
		db.query(sql);
		ResultSet rs = db.getRs();
		try {
			if(rs.next()) {
				userinfo.setId(rs.getInt(1));
				userinfo.setUsername(rs.getString(2));
				userinfo.setPassword(rs.getString(3));
				userinfo.setQuestion(rs.getString(4));
				userinfo.setAnswer(rs.getString(5));
				userinfo.setType(rs.getString(6));
			}else {
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeRs();
	        db.closeStm();
	        db.closeConn();
	    }		
		return userinfo;
	}

	@Override
	public boolean modifyUser(User oldUser, User newUser) {
		DBAccess db = new DBAccess();
		db.createConn();
		String sql = "update user set username='" + newUser.getUsername() + "', password='" + newUser.getPassword() + 
				"',question='" + newUser.getQuestion() + "', answer='"+ newUser.getAnswer()+"', type='" + newUser.getType() + 
				"' where username='" + oldUser.getUsername() + "';";
		if (db.update(sql)) {
			db.closeRs();
			db.closeStm();
			db.closeConn();
			return true;
		} else {
			db.closeRs();
			db.closeStm();
			db.closeConn();
			return false;
		}
	}

	@Override
	public boolean deleteUser(int id) {
		// TODO Auto-generated method stub
		boolean flag = false;
		DBAccess db = new DBAccess();
		String sql = "delete from user where id='" + id + "'";
		if(db.createConn()){
			flag = db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return flag;
	}

	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		DBAccess db = new DBAccess();
		String sql = "insert into user(username,password,question,answer,type) values('"+user.getUsername()+
				"','"+user.getPassword()+"','"+user.getQuestion()+"','"+user.getAnswer()+"','"+user.getType()+"')";
		if(db.createConn()){
			flag = db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return flag;
	}

	@Override
	public boolean findSameName(String username) {
		// TODO Auto-generated method stub
		boolean flag = false;
		DBAccess db = new DBAccess();
		String sql = "select * from user where username='"+username+"'";
		if(db.createConn()){
			db.query(sql);
			ResultSet rs = db.getRs();
			try {
				if(rs.next())	flag = true;
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
			db.closeStm();
			db.closeRs();
			db.closeConn();
		}
		return flag;
	}
	
	@Override
	public User checkUserForget(User user) {
		// TODO Auto-generated method stub
		DBAccess db = new DBAccess();
		User userinfo = new User();
		String sql =  "select * from user where username='" + user.getUsername() + "' and question='"
				+ user.getQuestion() + "' and answer='" + user.getAnswer() + "'";
		if(db.createConn()){
			db.query(sql);
			ResultSet rs = db.getRs();
			try {
				if(rs.next()) {
					userinfo.setId(rs.getInt(1));
					userinfo.setUsername(rs.getString(2));
					userinfo.setPassword(rs.getString(3));
					userinfo.setQuestion(rs.getString(4));
					userinfo.setAnswer(rs.getString(5));
					userinfo.setType(rs.getString(6));
				}else {
					return null;
				}
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
			db.closeStm();
			db.closeRs();
			db.closeConn();
		}
		return userinfo;
	}
	

	@Override
	public User findPassword(String username) {
		// TODO Auto-generated method stub
		return null;
	}

}
